package com.atguigu.gmall.cart.api;


import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;


@RequestMapping("/api/cart")
@RestController
public class CartController {


    @Autowired
    CartService cartService;

    @DeleteMapping("/deleteSku")
    Result deleteSkuFromCart(@RequestBody  List<String> skuIds,HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        if(!CollectionUtils.isEmpty(skuIds)){

            cartService.deleteCartItemBatch(key,skuIds);
        }


        return Result.ok();
    };



    /**
     * 响应式编程，只需要少量的线程能构建高并发的应用。
     * netty：  boss收请求。selector
     * @return
     */
    @GetMapping("/hhhhh")
    @ResponseBody
    public Flux<String> hello(){
        List<String> a = new ArrayList<>();
        CompletableFuture.runAsync(()->{

            while (true){
                a.add(UUID.randomUUID().toString());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }

        });

        return Flux.fromIterable(a);
    }


    //DELETE  /api/cart/deleteCart/51
    @DeleteMapping("/deleteCart/{skuId}")
    public Result ajaxDeleteCartItem(@PathVariable("skuId") Long skuId,
                                     HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        cartService.deleteCartItem(key,skuId);

        return Result.ok();
    }


    ///api/cart/checkCart/51/0
    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result ajaxCheckCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("checked") Integer checked,
                                HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        cartService.checkCartItem(key,skuId,checked);

        return Result.ok();

    }

    /**
     * ajax添加购物车
     * @param skuId
     * @param skuNum
     * @param request
     * @return
     */
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result ajaxAddToCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("skuNum") Integer skuNum,
                                HttpServletRequest request){
        //ajax请求过了网关。网关会自动提取并透传用户id以及临时id
        String key = cartService.determinCartKey(request);
        CartInfo cartInfo = cartService.addToCart(key, skuId, skuNum);


        return Result.ok();
    }
    /**
     * 表示把哪个商品添加到购物车
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/addToCart")
    public CartInfo addToCart(@RequestParam("skuId") Long skuId,
                              @RequestParam("skuNum") Integer skuNum,
                              HttpServletRequest request){

        /**
         * 告诉我们操作哪个用户的购物车
         * 用户登录了；有Cookie
         *      token：
         * 用户没登录：有Cookie
         *      userTempId：
         *
         */
        //操作redis的完整key
        String key = cartService.determinCartKey(request);
        CartInfo skuInfo = cartService.addToCart(key,skuId,skuNum);

        return skuInfo;
    }

    /**
     * /api/cart/cartList
     * 返回购物车中所有商品
     * @return
     */
    @GetMapping("/cartList")
    public Result listCart(HttpServletRequest request){

        String tempCartKey = cartService.tempCartKey(request);
        String userCartKey = cartService.userCartKey(request);
        //查询所有购物车数据
        List<CartInfo> cartInfos = cartService.listCart(userCartKey,tempCartKey);


        //把查到的购物车数据返回
        return Result.ok(cartInfos);

    }


    /**
     * 获取购物车中已经选中的商品
     * @return
     */
    @GetMapping("/cartListChecked")
    public List<CartInfo> cartInfoListCheck(HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        return cartService.listCheckCartInfo(key);
    }
}
